using System;
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using RMH.DataAccess;
using RMH.Entities;
using RMH.Services;
using RMH.Services.MembershipProvider;
using RMH.Web.RMHMembership;

namespace RMH.UnitTests.Membership
{
 
    [TestFixture]
    public class RMHRoleProvider_when_creating_a_role : base_automock_test
    {
        private string roleName = "Rad";
        
        public override void because()
        {
            Create<RMHRoleProvider>().CreateRole(roleName);
        }

        [Test]
        public void a_role_is_persisted()
        {
            Mock<IStaffRoleRepository>()
                .Verify(s=>s.Save(It.Is<StaffRole>(r=>r.Name == roleName)));
                
        }

    }
    [TestFixture]
    public class RMHRoleProvider_when_adding_user_to_role : base_automock_test
    {
        List<String> names = new List<string> { "ignu" };
        List<String> roles = new List<string> { "Administrator" };
        
        public override void establish_context()
        {
            Mock<IStaffService>()
                .Setup(s => s.GetByUserName("ignu"))
                .Returns(new Staff());

            Mock<IStaffRoleRepository>()
                .Setup(s => s.GetByName(roles[0]))
                .Returns(new StaffRole());

        }
        public override void because()
        {
            Create<RMHRoleProvider>()
                .AddUsersToRoles(names.ToArray(), roles.ToArray());
        }

        [Test]
        public void canresolvestaffservice()
        {
            Container.Resolve<IStaffService>();
        }
        [Test]
        public void user_with_new_role_is_saved()
        {
            Mock<IStaffService>()
                .Verify(ss=>ss.Save(It.Is<Staff>(st=>st.Roles.Count == 1)));
        }
    }

    [TestFixture]
    public class RMHRoleProvider_when_detecting_if_a_role_exists : base_automock_test
    {

        private bool result;

        public override void establish_context()
        {
            Mock<IStaffRoleRepository>()
                .Setup(s => s.GetByName("Dumbass"))
                .Returns(new StaffRole{Id = 4});
        }

        public override void because()
        {
            result = Create<RMHRoleProvider>().RoleExists("Dumbass");
        }

        [Test]
        public void returns_false_if_role_is_in_repository()
        {
            result.ShouldBeTrue();
        }

    }


    [TestFixture]
    public class RMHRoleProvider_when_getting_users_roles : base_automock_test
    {
        private string[] roles;
        private const string roleName = "Administrator";

        public override void establish_context()
        {
            var roles = new List<StaffRole> {new StaffRole {Name = roleName}};

            Mock<IStaffService>()
                .Setup(s => s.GetByUserName("ignu"))
                .Returns(new Staff{Roles = roles});
        }

        private RMHRoleProvider roleProvider;
        public override void because()
        {
            roleProvider = Create<RMHRoleProvider>();
        }

        [Test]
        public void gets_login_list_from_repository()
        {
            roleProvider.GetRolesForUser("ignu").ShouldContain(roleName);
        }

        [Test]
        public void is_in_roll()
        {
            roleProvider.IsUserInRole("ignu", roleName).ShouldBeTrue();
        }
    }
}